﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace CourseBook
{
    public partial class LearnLevel : System.Web.UI.Page
    {
        
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlConnection con;
        int txtid;
        string str = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LearnLevel_Load();
            }
        }

        private void LearnLevel_Load()
        {
            ds = new DataSet();
            using (con = new SqlConnection(str))
            {
                con.Open();
                cmd = new SqlCommand("LearnLevel_Load", con);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
                gvLearnLevel.DataSource = ds.Tables[0];
                gvLearnLevel.DataBind();
            }
        }

        protected void nhap_click(object sender, EventArgs e)
        {
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("LearnLevel_Insert", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@LearnLevelName", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@LearnLevelNote", SqlDbType.NVarChar).Value = txtNote.Text;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                { }

                LearnLevel_Load();
            }
        }

        protected void refresh_click(object sender, EventArgs e)
        {
            btnRefresh.CausesValidation = true;
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            txtName.Text = "";
            txtNote.Text = "";
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void sua_click(object sender, EventArgs e)
        {
            int key = (int)gvLearnLevel.DataKeys[gvLearnLevel.SelectedIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("LearnLevel_Update", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = key;
                    cmd.Parameters.Add("@LearnLevelName", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@LearnLevelNote", SqlDbType.NVarChar).Value = txtNote.Text;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                { }
                LearnLevel_Load();
            }
        }

        protected void xoa_click(object sender, EventArgs e)
        {
            
        }

        protected void Cancel_click(object sender, EventArgs e)
        {
            
        }

        protected void gvLearnLevel_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int key = (int)gvLearnLevel.DataKeys[e.RowIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("LearnLevel_Delete", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = key;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                {

                }

                LearnLevel_Load();
            }
        }

        protected void gvLearnLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            sua.Enabled = true;
            int dong = gvLearnLevel.SelectedIndex;
            txtName.Text = gvLearnLevel.Rows[dong].Cells[2].Text.ToString();
            txtNote.Text = gvLearnLevel.Rows[dong].Cells[3].Text.ToString();
        }
    }
}